//
//  EMTabBarController.m
//  EasyMoney
//
//  Created by zh on 2019/12/14.
//  Copyright © 2019 zh. All rights reserved.
//

#import "EMTabBarController.h"
#import "EMLoanViewController.h"
#import "EMRepaymentViewController.h"
#import "EMMineViewController.h"
#import "EMNavigationController.h"
#import "EMAgreementViewController.h"

static EMTabBarController* __instance = nil;
@interface EMTabBarController ()<UITabBarControllerDelegate>
{
    NSMutableDictionary* _publicParams;
}

@property (nonatomic,assign) NSUInteger selectIndex;

@property (nonatomic, strong) NSMutableArray* vcArray;

@end

@implementation EMTabBarController

+(instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __instance = [[EMTabBarController alloc] init];
    });
    return __instance;
}

- (NSMutableArray *)vcArray
{
    if (!_vcArray) {
        _vcArray = [NSMutableArray array];
    }
    return _vcArray;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.delegate = self;
        _selectIndex = 0;
    }
    return self;
}

+ (BOOL)isSingleton
{
    return YES;
}

- (BOOL)handleWillOpenUrlWithParams:(NSDictionary*)params {
    NSString* page = [params objectForKey:@"page"];
    if ([page isEqualToString:@"1"]) {
        _selectIndex = 0;
        self.selectedIndex = _selectIndex;
        return YES;
    }
    
    if ([page isEqualToString:@"4"]) {
        _selectIndex = 1;
        self.selectedIndex = _selectIndex;
        return YES;
    }
    
    if ([page isEqualToString:@"2"]) {
        _selectIndex = 1;
        self.selectedIndex = _selectIndex;
        return YES;
    }
    
    if ([page isEqualToString:@"3"]) {
        _selectIndex = 2;
        self.selectedIndex = _selectIndex;
        return YES;
    }
    return YES;
}

-(void)setPublicParams:(NSDictionary*)params
{
    if (_publicParams == nil) {
        _publicParams = [[NSMutableDictionary alloc]init];
    }
    [_publicParams addEntriesFromDictionary:[params copy]];
}

-(NSDictionary*)getPublicParams
{
    return [_publicParams copy];
}

- (void)handleTransferPublicParams:(NSDictionary*)params {
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([EMAgreementViewController needShowAgreement]) {
         EMAgreementViewController *agree = [[EMAgreementViewController alloc] init];
         [self.navigationController pushViewController:agree animated:YES];
     }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)rightTopAction:(id)sender
{
    //1. 判断之前是否存在token有的话传token->更换token登录完跳转首页
    //2. 无token则为第一次登录，登录完毕后回此页面
    
    if (self.selectIndex == 0) {
        if (SAFE_STRING([[EMNetConfig sharedConfig] getToken]).length == 0) {
            ICEOpenUrl(@"emoney://login?fromoriginvc=EMTabBarController");
        }
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.tabBar setBackgroundImage:[UIColor imageWithColor:[UIColor whiteColor]]];
    //    [self.tabBar setShadowImage:[UIColor createImageWithColor:WDSD_e2_COLOR]];  //改变tabBar分割线显色
    
    EMLoanViewController* homeVC = [[EMLoanViewController alloc] init];
    [self addOneChildVC:homeVC Title:NSLocalizedString(@"首页", nil) ImageName:@"bar_home_done" SelectedImageName:@"bar_home_done_select"];
    
    EMRepaymentViewController *recordVC = [[EMRepaymentViewController alloc] init];
    [self addOneChildVC:recordVC Title:NSLocalizedString(@"还款", nil) ImageName:@"bar_order" SelectedImageName:@"bar_order_select"];
    

    EMMineViewController *mine = [[EMMineViewController alloc] init];
    [self addOneChildVC:mine Title:NSLocalizedString(@"我的", nil) ImageName:@"bar_my" SelectedImageName:@"bar_my_select"];
    self.selectedIndex = 0;
    self.viewControllers = self.vcArray;
    [self.tabBar setShadowImage:[UIColor imageWithColor:[UIColor colorwithHexString:@"#000000" alpha:0.05]]];
}

- (void)addOneChildVC:(UIViewController *)childVC Title:(NSString *)title ImageName:(NSString *)imageName SelectedImageName:(NSString *)selectedImageName
{
    // 1.设置子控制器的属性
    childVC.view.backgroundColor = [UIColor whiteColor];
    childVC.title = title;
    
    UIImage *norImage = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImage = [[UIImage imageNamed:selectedImageName ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    childVC.tabBarItem.image = norImage;
    childVC.tabBarItem.selectedImage = selectedImage;
    
    [self tabarSetTitleColorWithViewController:childVC];
    
    [self.vcArray addObject:childVC];
    
}

-(void)tabarSetTitleColorWithViewController:(UIViewController*)vc
{
    [[vc tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             ColorFromRGB(0x2f5aff), NSForegroundColorAttributeName,FONT_PINGFANG_REGULAR_(11),NSFontAttributeName,
                                             nil] forState:UIControlStateSelected];
    [[vc tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             ColorFromRGB(0xc2c2c2), NSForegroundColorAttributeName,FONT_PINGFANG_REGULAR_(11),NSFontAttributeName,
                                             nil] forState:UIControlStateNormal];
    
    UIEdgeInsets insets=UIEdgeInsetsMake(-2, 0, 2, 0);
    [vc tabBarItem].imageInsets =insets;
    [vc tabBarItem].titlePositionAdjustment = UIOffsetMake(0, -2);
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    switch ([tabBarController.childViewControllers indexOfObject:viewController]) {
        case 0:
            //首页
            break;
        case 1:
           //还款
            break;
        case 2:
            //我的
            if (SAFE_STRING([[EMNetConfig sharedConfig] getToken]).length > 0) {
                //有 token
                return YES;
            }
            else {
                ICEOpenUrl(@"emoney://login?fromoriginvc=EMTabBarController");
                return NO;
            }
            break;
            
        default:
            break;
    }
    return YES;
}

+(NSString *)relyingOnOtherUrlSchemeWithParams:(NSDictionary *)params
{
    if ([[params objectForKey:@"page"] isEqualToString:@"1"]) {
        return @"";
    }
    
    if (SAFE_STRING([[EMNetConfig sharedConfig] getToken]).length > 0) {
        //有 token
        return @"";
    }
    return @"emoney://login?fromoriginvc=EMTabBarController";
}


@end
